<?php
	//Initial date: 2009-05-31
	//Last change: 2009-05-31 by Kamil Jaskiewicz

	include("classes/DBTable.class.php");
	include("classes/contacts.class.php"); 
	include("classes/departments.class.php");

	$module_name = "Contacts";
	$action = $_REQUEST['action'];
	$contacts = new contacts();

	if ($_REQUEST['submit'])
	{
		$id		= $_REQUEST['id'];
		$c_fn	= $_REQUEST['contact_first_name'];
		$c_ln	= $_REQUEST['contact_last_name'];
		$c_d_id = $_REQUEST['contact_dept_id'];

		if ($action == 'edit')
		{
			$contacts->DB->query("UPDATE contacts SET contact_first_name='$c_fn', contact_last_name='$c_ln', 
				contact_dept_id='$c_d_id' WHERE contact_id='$id'");

		} 
		else if ($action == 'add')
		{
			$q = "INSERT INTO contacts VALUES ('', '$c_fn', '$c_ln', '$c_d_id')";
			$contacts->DB->query($q);			
			$id = $contacts->DB->LastID;
		}
		header("location:contacts.php");
		exit();
	}

	if ($action == 'delete')
	{
		$id = $_REQUEST['id'];
		if($id)
		{
			$contacts->delete($id);
			header("location:contacts.php");
			exit();
		}
	}


	include("header.php");
	

	//action not defined, display all projects
	if (!$action)
	{
		$contacts->selectAll();

		print("<table id='grid'>");
		print("<tr><td><b>Contact</b></td><td><b>Company</b></td><td><b>Department</b></td><td colspan='3'></td></tr>\n");
		while($contacts->getNext())
		{
			print("<tr>");
			print("<td><a href='contacts.php?action=view&id=$contacts->contact_id'>".str_replace(" ", "&nbsp;", $contacts->contact_first_name." ".$contacts->contact_last_name)."</a></td>");
			print("<td>".str_replace(" ", "&nbsp;", $contacts->company_name)."</td>");
			print("<td>".str_replace(" ", "&nbsp;", $contacts->department_name)."</td>");
			print("<td><a href='contacts.php?action=edit&id=$contacts->contact_id'>Edit</a></td>");
			print("<td><a href='contacts.php?action=delete&id=$contacts->contact_id' onclick='return confirmation(\"Are you sure you want to delete this contact?\");' >Delete</a></td>");
			print("<td style='width:80%'>&nbsp;</td>");
			print("</tr>\n\n");
		}
		print("<tr><td colspan='6' style='text-align:right'><a href='contacts.php?action=add'>Add new contact</a></td></tr>");
		print("</table>");
	}


	//display details of the project
	if ($action == 'view')
	{
		$id=$_REQUEST['id'];
		$contacts->findID($id);

		print("<table id='grid'>");
		print("<tr><td><b>First name</b></td><td>$contacts->contact_first_name</td></tr>\n");
		print("<tr><td><b>Last name</b></td><td>$contacts->contact_last_name</td></tr>\n");
		print("<tr><td><b>Company</b></td><td>$contacts->company_name</td></tr>\n");
		print("<tr><td><b>Department</b></td><td>$contacts->department_name</td></tr>\n");
		print("<tr><td colspan='2'><a href='contacts.php?action=edit&id=$contacts->contact_id'>Edit</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='contacts.php?action=delete&id=$contacts->contact_id' onclick='return confirmation(\"Are you sure you want to delete this contact?\");' >Delete</a></td></tr>\n");
		print("</table>");

	}

	if ($action == 'edit' || $action == 'add')
	{
		$id = $_REQUEST['id'];
		if ($id)
			$contacts->findID($id);

		$dept = new departments();
		print("<form action='contacts.php' method='post'>");
		if ($action == 'edit')
		{
			print("<input type='hidden' name='action' value='edit' />");
			print("<input type='hidden' name='id' value='$id' />");
		}
		else if ($action == 'add')
		{
			print("<input type='hidden' name='action' value='add' />");
		}
		print("<input type='hidden' name='submit' value='true' />");
		print("<table id='grid'>");
		print("<tr><td><b>First name</b></td><td><input type='text' value='$contacts->contact_first_name' name='contact_first_name'/></td></tr>\n");
		print("<tr><td><b>Last name</b></td><td><input type='text' value='$contacts->contact_last_name' name='contact_last_name'/></td></tr>\n");		
		print("<tr><td><b>Department</b></td><td><select name='contact_dept_id'>\n");
		$dept->selectAll();
		while($dept->getNext())
		{
			if ($dept->dept_id != $contacts->contact_dept_id)
				print("<option value='$dept->dept_id'>$dept->dept_name in $dept->company_name</option>\n");
			else
				print("<option selected value='$dept->dept_id'>$dept->dept_name in $dept->company_name</option>\n");
		}
		print ("</select></td></tr>\n");
		print("<tr><td colspan='2'><input type='submit' value='Submit' />&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' value='Cancel' onclick='history.back();' /></td></tr>\n");
		print("</table>");
		print("</form>");
	}
	
	include("footer.php");
?>